package yb.ecp.fast.infra.util;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

public class PageHelperPlus extends PageHelper {

    public static void orderBy(String orderBy) {
        Page<?> page = getLocalPage();
        if (page != null) {
            page.setUnsafeOrderBy(orderBy);
        } else {
            page = new Page();
            page.setUnsafeOrderBy(orderBy);
            page.setOrderByOnly(true);
            setLocalPage(page);
        }
    }

    /**
     * 开始分页
     *
     * @param pageNum  页码
     * @param pageSize 每页显示数量
     */
    public static <E> Page<E> startPage(int pageNum, int pageSize) {
        if (pageNum == 0) {
            pageNum = 1;
            pageSize = 1000;
        }
        if (pageSize == 0) {
            pageSize = 1000;
        }
        return startPage(pageNum, pageSize, DEFAULT_COUNT, null, null);
    }

    public static <E> Page<E> startPage(int pageNum, int pageSize, boolean count, Boolean reasonable, Boolean pageSizeZero) {
        Page<E> page = new Page<E>(pageNum, pageSize, count);
        page.setReasonable(reasonable);
        page.setPageSizeZero(pageSizeZero);
        //当已经执行过orderBy的时候
        Page<E> oldPage = getLocalPage();
        if (oldPage != null && oldPage.isOrderByOnly()) {
            page.setUnsafeOrderBy(oldPage.getOrderBy());
        }
        setLocalPage(page);
        return page;
    }
}
